<?php
	// 品牌模型
	class BrandService extends Service {
	
		/**
		 * 查询品牌。
		 * @param unknown_type $map
		 * @param unknown_type $limit
		 */
		public function search4UserBrand($map,$limit=10){
			if(!empty($map['bscode'])&&$map['bscode']!='all')
					$where['sort_id']	=$map['bscode'];
			$where['parent_id'] = 0;
			$where['enable'] 	= 'Y';
			$where['is_delete'] = 'N';
			
			$order=' ctime desc ';
			$partnerDao = M('Partner');
			
			$pagedata = $partnerDao->kPageQuery($where,$order,$limit);
			return $pagedata;
		}
		
		/**
		 * 查询展出的品牌。随机连续读取制定个数的品牌数据。
		 * Enter description here ...
		 */
		public function selectShowBrand($showNo=5){
			$partnerDao = M('Partner');
			$where['parent_id'] = 0;
			$where['display'] 	= 'Y';
			$where['enable'] 	= 'Y';
			$where['is_delete'] = 'N';
			
			// 去掉统计时的排序提高效率
			$result =	$partnerDao->where($where)->limit(1)->field('count(1) as count')->find();
			$count	=	$result['count'];
			$startno=0;
			if($count>$showNo)$startno = rand(0,$count-$showNo);
			//echo $startno;
			$order=' ctime desc ';
			$showBrand = $partnerDao->where($where)->order($order)->limit($startno.','.$showNo)->findAll();
			return $showBrand;
		}
		
		/**
		 * 通过品牌id查询，可用的未删除的父信息
		 * @param String $brand_id
		 */
		public function getById($brand_id){
			$partnerDao = M('Partner');
			$ary['id']			=	$brand_id;
			$ary['parent_id'] 	= 	0;
			//$ary['open']		=	'Y';
			//$ary['display']	=	'Y';
			$ary['enable']		=	'Y';
			$ary['is_delete']	=	'N';
			$brand = $partnerDao->where($ary)->find();
			return $brand;
		}
		
		
		/**
		 * 查询品牌下的分店。
		 * @param unknown_type $brand_id
		 */
		public function search4UserBSub($brand_ids,$area_id=null,$city_id=null,$province_id=null,$limit=8){
			if(empty($brand_ids))return null;
			if(is_array($brand_ids)){
				$where['parent_id'] 	= 	array('in',$brand_ids);
			}else{
				$where['parent_id'] 	= 	$brand_ids;
			}
			
			if(!empty($area_id))$where['area_id'] 	= 	$area_id;
			if(!empty($city_id))$where['city_id'] 	= 	$city_id;
			if(!empty($province_id))$where['province_id'] 	= 	$province_id;
			$where['enable']	=	'Y';
			$where['is_delete']	=	'N';
			$order=' ctime desc ';
			$partnerDao = M('Partner');
			$bsubpage = $partnerDao->kPageQuery($where,$order,$limit);
			return $bsubpage;
		}
		
		//查询地点下的品牌
		public function selectBrandIdsByTag( $tag ,$brand_ids , $limit=null ){
			if(empty($tag))return null;
			$partnerDao = M('Partner');
			if(is_array($brand_ids)){
				$where['id'] 	= 	array('in',$brand_ids);
			}else{
				if(!empty($brand_ids))$where['id'] 	= 	$brand_ids;
			}
			$where['parent_id'] = array('NEQ',0);
			$where['tag'] = array('like',"%".$tag.",%");
			$where['display'] 	= 'Y';
			$where['enable'] 	= 'Y';
			$where['is_delete'] = 'N';

			$order=' parent_id asc ';
			$brands = $partnerDao->where($where)->field(" DISTINCT parent_id ")->order($order)->limit($limit)->findAll();
			//echo "<br>".$partnerDao->getLastSql();
			foreach($brands as $vo){
				$result[]=$vo['parent_id'];
			}
			return $result;
		}
		
		//查询地点下的品牌
		public function selectBSubsByTag( $tag ,$brand_ids , $limit=null ){
			//if(empty($tag))return null;
			if(is_array($brand_ids)){
				$where['id'] 	= 	array('in',$brand_ids);
			}else{
				if(!empty($brand_ids))$where['id'] 	= 	$brand_ids;
			}
			if(!empty($tag))$where['tag'] = array('like',"%".$tag.",%");
			//$order=' parent_id asc ';
			$where['parent_id'] = array('NEQ','0');
			$partners = $this->select4User($where,$field="id,parent_id,title,location,brief,brand_icon,phone,address,mobile",100);
			
			return $partners;
		}
		
		public function select4User($where,$field="*",$limit=10){
			$partnerDao = M('Partner');
			//$where['parent_id'] = 0;
			$where['display'] 	= 'Y';
			$where['enable'] 	= 'Y';
			$where['is_delete'] = 'N';

			$order=' parent_id asc ';
			$showBrand = $partnerDao->where($where)->field($field)->order($order)->limit($limit)->findAll();
			return $showBrand;
		}
		
		public function select4UserByPid($pid,$field="*"){
			$partnerDao = M('Partner');
			$where['id'] = $pid;
			$where['display'] 	= 'Y';
			$where['enable'] 	= 'Y';
			$where['is_delete'] = 'N';

			$brands = $this->select4User($where,$field,1);
			return $brands[0];
		}
		
		/**
		 * 通过商品分类查询品牌信息。
		 * @param Array $codes	商品分类组成的数组
		 */
		public function select4UserByGoodsSort($codes){
			if(empty($codes))return null;
			$map1['sort_id'] = array('in',$codes);
			$map1['enable'] = 'Y';
			$map1['is_delete'] = 'N';
			$pids = M('Goods')->where($map1)->field('partner_id')->findAll();
			$pids = getSubByKey($pids, 'partner_id');
			$map['id'] = array('in',$pids);
			$map['enable'] = 'Y';
			$map['is_delete'] = 'N';
			$listPartner = M('Partner')->where($map)->findAll();
			return $listPartner;
		}
	
		public function getExtInfoByBid($brandid){
			$brandExtInfo=M('PartnerInfo')->where('partner_id='.$brandid)->find();
			return $brandExtInfo;
		}
		
		//服务初始化
		public function init($data = '') {
		}
		
		
		//运行服务，系统服务自动运行
		public function run() {
		}
	}
?>